package org.dreamfly.netshop.actions.member;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.dreamfly.netshop.entity.Member;
import org.dreamfly.netshop.entity.OrderInfo;
import org.dreamfly.netshop.entity.ShopCart;
import org.dreamfly.netshop.manage.MemberManager;
import org.dreamfly.netshop.manage.OrderInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 用户管理Action. 使用Struts2 convention-plugin annotation定义Action参数.
 * 
 * @author calvin
 */
public class MemberAction extends ActionSupport {

    @Autowired
    private MemberManager memberManager;

//    @Autowired
//    private ShopCartManager shopCartManager;
//
//    @Autowired
//    private AddressManager addressManager;

    @Autowired
    private OrderInfoManager orderInfoManager;

    // @Autowired
    // private BuyInfoManager buyInfoManager;
    //
    // @Autowired
    // private GoodsInfoManager goodsInfoManager;

    private Member member;

    private List<OrderInfo> orderList = new ArrayList<OrderInfo>();

    private Long id;

    private String msg;

    private Page<ShopCart> shopCart = new Page<ShopCart>(10);

    public Page<ShopCart> getShopCart() {
        return shopCart;
    }

    public String getMsg() {
        return msg;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<OrderInfo> getOrderList() {
        return orderList;
    }

    public void setOrderList(List<OrderInfo> orderList) {
        this.orderList = orderList;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public String execute() {
        // ActionContext ctx = ActionContext.getContext();
        // HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        // HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);
        // classManager.delete((long) 21);
        return SUCCESS;
    }

    public String checkLogin() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        // String codeO = (String) session.getAttribute("jpeg-random-code");
        // String codeI = request.getParameter("code");
        // if (!codeI.equals(codeO)) {
        // request
        // return NONE;
        // }

        if (member == null || member.getName() == null || member.getPassword() == null || "".equals(member.getName())
                || "".equals(member.getPassword())) {
            try {
                ServletActionContext.getResponse().sendRedirect(request.getContextPath());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return NONE;
        }
        member = memberManager.CheckUserAndPwd(member);
        if (member == null) {
            try {
                ServletActionContext.getResponse().sendRedirect(request.getContextPath());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return NONE;
        }
        session.setAttribute("member", member);
        request.setAttribute("needToPay", orderInfoManager.getAllNeedToPay(member.getId()).size());
        request.setAttribute("needToShip", orderInfoManager.getNeedToShip(member.getId()).size());
        request.setAttribute("needToSure", orderInfoManager.getNeedToSure(member.getId()).size());
        request.setAttribute("success", orderInfoManager.getAllSuccess(member.getId()).size());
        return SUCCESS;
    }

    public String register() {
        member.setLoadDate(new Date());
        memberManager.save(member);
        ServletActionContext.getRequest().getSession().setAttribute("member", member);
        myAccount();
        return msg;
    }

    public String myAccount() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        request.setAttribute("needToPay", orderInfoManager.getAllNeedToPay(member.getId()).size());
        request.setAttribute("needToShip", orderInfoManager.getNeedToShip(member.getId()).size());
        request.setAttribute("needToSure", orderInfoManager.getNeedToSure(member.getId()).size());
        request.setAttribute("success", orderInfoManager.getAllSuccess(member.getId()).size());
        return SUCCESS;
    }

    public String logout() {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        session.setAttribute("member", null);
        try {
            ServletActionContext.getResponse().sendRedirect(request.getContextPath());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return NONE;
    }

    public String needToPay() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        orderList = orderInfoManager.getAllNeedToPay(member.getId());
        return "needtopay";
    }

    public String needToShip() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        orderList = orderInfoManager.getNeedToShip(member.getId());
        return "needtoship";
    }

    public String needToSure() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        orderList = orderInfoManager.getNeedToSure(member.getId());
        return "needtosure";
    }

    public String getSuccessShopList() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member member = (Member) request.getSession().getAttribute("member");
        orderList = orderInfoManager.getAllSuccess(member.getId());
        return "suc";
    }

    public String getMemberInfo() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member mem = (Member) request.getSession().getAttribute("member");
        member = memberManager.get(mem.getId());
        return "info";
    }

    public String getQuestion() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member mem = (Member) request.getSession().getAttribute("member");
        member = memberManager.get(mem.getId());
        return "pwd";
    }

    public String modifyMemberInfo() {
        Member mem = memberManager.get(member.getId());
        mem.getName();
        mem.setTrueName(member.getTrueName());
        mem.setAddress(member.getAddress());
        mem.setEmails(member.getEmails());
        mem.setMobile(member.getMobile());
        mem.setPhone(member.getPhone());
        mem.setPostCode(member.getPostCode());
        mem.setSex(member.getSex());
        memberManager.save(mem);
        member = mem;
        // member = mem;
        msg = "修改成功";
        return "info";
    }

    public String modifyPassword() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member mem = (Member) request.getSession().getAttribute("member");
        mem = memberManager.get(mem.getId());
        String newp = request.getParameter("newp");
        String newp2 = request.getParameter("newp2");
        if (mem.getPassword() == null || !mem.getPassword().equals(member.getPassword())) {
            msg = "密码输入错误！";
        } else if (newp == null || newp2 == null) {
            msg = "密码不能为空！";
        } else if (!newp.equals(newp2)) {
            msg = "两次密码输入不一致！";
        } else {
            mem.setPassword(newp);
            memberManager.save(mem);
            member = mem;
            msg = "修改成功！";
        }
        return "pwd";
    }

    public String modifyQAA() {
        HttpServletRequest request = ServletActionContext.getRequest();
        Member mem = (Member) request.getSession().getAttribute("member");
        mem = memberManager.get(mem.getId());
        String newq = request.getParameter("newq");
        String newa = request.getParameter("newa");
        if (mem.getQuestions() != null
                && (member.getAnswers() == null || !mem.getAnswers().equals(member.getAnswers()))) {
            msg = "问题答案错误！";
        } else if (newq == null || newa == null || "".equals(newa) || "".equals(newq)) {
            mem.setQuestions(null);
            mem.setAnswers(null);
            memberManager.save(mem);
            msg = "清除成功！";
        } else {
            mem.setQuestions(newq);
            mem.setAnswers(newa);
            memberManager.save(mem);
            msg = "修改成功！";
        }
        member = mem;
        return "pwd";
    }

    public String getGoods() {
        OrderInfo orderInfo = orderInfoManager.get(id);
        orderInfo.setIsConfirm(true);
        return myAccount();
    }
}
